package arm_search_3.query.email;

import arm_search_3.crawling.email.EmailCrawler;
import arm_search_3.data.QueryModel;
import arm_search_3.data.ResultModel;
import arm_search_3.data.Term;
import arm_search_3.query.QueryProcessor;
import arm_search_3.utilities.TermHelper;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.PriorityQueue;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author
 */
public class EmailQueryProcessor implements QueryProcessor {

    private HashMap<Integer, String> mapping;
    private Integer queryNumber;

    public void writeResultToFile(PriorityQueue<ResultModel> result, String query, Integer n_top, long elapsedTime) {
        try {
            queryNumber++;
            BufferedWriter out = new BufferedWriter(new FileWriter(new File("ARM-Q"+queryNumber+".txt")));
            int iter = 0;
            while (!result.isEmpty() && (iter < n_top)) {
                iter++;
                ResultModel model = result.poll();
                out.write("Q"+queryNumber+" "+mapping.get(model.getDocumentID())+" "+iter+" "+model.getWeight()+"\n");
            }
            out.write(""+elapsedTime);
            out.close();
        } catch (IOException ex) {
            Logger.getLogger(EmailQueryProcessor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void initQueryProcessor(String indexDirectory, boolean isCompressed) {
        // load file mapping antara id dokumen dengan id message
        EmailCrawler crawler = new EmailCrawler();
        crawler.readDocMappingFile(indexDirectory, isCompressed);
        mapping = crawler.getDocumentMapping();
        queryNumber = 0;
    }

    @Override
    public void executeQuery(String indexDirectory, String query, boolean isCompressed, Integer n_top) {
        ArrayList<HashMap<Integer, ResultModel>> queryResultCollection = new ArrayList<>();
        long start = System.currentTimeMillis(), end;
        // pecah query menjadi query models
        ArrayList<QueryModel> queries = QueryModel.splitString(query);
        // untuk tiap query models : 
        for (QueryModel queryModel : queries) {
            ArrayList<HashMap<Integer, ArrayList<Integer>>> postingListCollection = new ArrayList<>();
            // untuk tiap terms : 
            ArrayList<String> termStrings = queryModel.getTerms();
            for (String termString : termStrings) {
                Term termObject = TermHelper.getTermObject(indexDirectory, termString, isCompressed);
                postingListCollection.add(TermHelper.getPostingList(termObject, queryModel.getField()));
            }
            queryResultCollection.add(TermHelper.processPostingLists(postingListCollection, queryModel.isIsPhrase()));
        }
        end = System.currentTimeMillis();
        writeResultToFile(TermHelper.processResultModels(queryResultCollection), query, n_top, end - start);
    }
}